package com.example.demo.service;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.bean.CmsArticle;
import com.example.demo.bean.CmsComment;
import com.example.demo.bean.CmsMember;
import com.example.demo.mapper.CmsArticleMapper;
import com.example.demo.mapper.CmsCommentMapper;

@Service
public class CmsCommentService {
	
	@Autowired
	private CmsCommentMapper cmsCommentMapper;
	
	@Autowired
	private CmsArticleMapper cmsArticleMapper;
	
	public List<CmsComment> selectAll() {
		return cmsCommentMapper.selectAll();
	}
	
	public CmsComment selectById(String id) {
		return cmsCommentMapper.selectByPrimaryKey(id);
	}
	
	public int deleteById(String id) {
		CmsComment cmsComment= cmsCommentMapper.selectByPrimaryKey(id);
		if (cmsComment!=null) {
			return cmsCommentMapper.deleteByPrimaryKey(id);
		}
		return -1;
	}
	
	public int batchDeleteByIds(String[] ids)
	{
		if (ids!=null && ids.length>0) {
			return cmsCommentMapper.batchDeleteByIds(ids);
		}
		return 0;
	}
	
	public int saveOrUpdate(CmsComment cmsComment)
	{
		CmsArticle cmsArticle =  cmsArticleMapper.selectByPrimaryKey(cmsComment.getArticleId());
		
		if (cmsArticle!=null) {
			if (cmsComment.getId() !=null) {
				CmsComment cms= cmsCommentMapper.selectByPrimaryKey(cmsComment.getId());
				if (cms==null) {
					return -1;
				}
				cmsCommentMapper.updateByPrimaryKey(cmsComment);
				return 1;
			}
			else {
				cmsComment.setId(UUID.randomUUID().toString());
				cmsCommentMapper.insert(cmsComment);
			}
			return 1;
			
		}
		
		return -2;

	}
	
}
